<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Community;
use App\Models\CommunityAddress;
use App\Models\CommunityGoods;
use App\Models\CommunityShop;
use App\Models\Order;
use App\Models\Renting;
use App\Service\CommonService;
use Illuminate\Http\Request;

class CommunityController extends Controller
{
    public function getUserCommunity()
    {
        $latitude = \request()->get('latitude');
        $longitude = \request()->get('longitude');
        if (empty($latitude) || empty($longitude)) return CommonService::fail(9999, '参数不能为空');
        try {
            $communityName = CommonService::curlGetCommunity($latitude,$longitude);
            $communityDb = Community::where('community_name', $communityName)->get()->toArray();
            if (!$communityDb) return CommonService::fail(9999, '您的社区不在服务内');
            $communityId = $communityDb[0]['id'];//获取社区的id
            $communityGoodsDb = CommunityShop::where('community_id', $communityId)->get()->toArray();
            if (!$communityGoodsDb) return CommonService::fail(9999, '您当前社区没有出售商品');
            $communityGoodsDb = Community::with(['goods'])->get()->toArray();
            return CommonService::success(200, '获取数据', $communityGoodsDb);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

    public function addShoppingCart()
    {
        try {
            $goodsId = intval(\request()->get('goods_id'));
            $uid = intval(\request()->get('uid'));
            $goodsDb = CommunityGoods::where('id', $goodsId)->first();
            if (!$goodsDb) return CommonService::fail(9999, '商品不存在');
            $goodsDb = $goodsDb->toArray();
            $addressDb = CommunityAddress::where('user_id', $uid)->first();
            if (!$addressDb) return CommonService::fail(9999, '请设置您的收货地址');
            $addressDb = $addressDb->toArray();
            $shopDb = CommunityShop::where('id', $addressDb['shop_id'])->first()->toArray();
            $insertDb = [
                'order_id' => rand(1111, 9999) . time(),
                'uid' => $uid,
                'user_address' => $shopDb['shop_name'],
                'cart_id' => 0,
                'freight_price' => 0,
                'total_num' => 1,
                'total_price' => $goodsDb['goods_price'],
                'total_postage' => 0,
                'pay_price' => $goodsDb['goods_price'],
                'pay_postage' => 0,
                'deduction_price' => 0,
                'coupon_id' => 0,
                'coupon_price' => 0,
                'paid' => 0,
                'pay_type' => '',
                'status' => 0,
                'refund_status' => 0,
                'delivery_name' => $shopDb['shop_name'] . '的自提点快递',
                'delivery_type' => 0,
                'mark' => '',
                'combination_id' => 0,
                'pink_id' => 0,
                'seckill_id' => 0,//秒杀业务
                'bargain_id' => 0,
                'store_id' => $shopDb['id'],
                'shipping_type' => 2,
                'goods_id' => $goodsId,
                'created_at' => date('Y-m-d H:i:s')
            ];
            $a = Order::insert($insertDb);
            dd($a);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

    public function selectAddress()
    {
        $uid = \request()->get('uid');
        $communityName = \request()->get('address');
        $communityDb = Community::where('community_name',$communityName)->first();
        if (empty($communityDb))return CommonService::fail(9999,'您的社区有误，或已下线');
        $communityDb = $communityDb->toArray();
        $communityId = $communityDb['id'];
        $shopDb = CommunityShop::where('community_id',$communityId)->get()->toArray();
        return CommonService::success(200,'获取门店列表',$shopDb);
    }
    public function settingAddress()
    {
        $uid = \request()->get('uid');
        $shopId = \request()->get('shop_id');
        if (empty($uid) || empty($shopId))return CommonService::fail(9999,'参数不能为空');
        try {
            $addressDb = CommunityAddress::where('user_id',$uid)->where('shop_id',$shopId)->first();
            if (!$addressDb){
                CommunityAddress::insert([
                    'user_id' => $uid,
                    'shop_id' => $shopId,
                ]);
                return CommonService::success(200,'添加门店成功');
            }
            $addressDb = $addressDb->toArray();
            CommunityAddress::where('user_id',$uid)->update($addressDb);
            return CommonService::success(200,'修改门店成功');
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }
}
